//
//  BrideSaidView.m
//  wedding
//
//  Created by 常宽 on 15/1/24.
//  Copyright (c) 2015年 常宽. All rights reserved.
//
/*****新娘说*****/
#import "BrideSaidView.h"
#import "BrideSaidModel.h"
#import "BSUres.h"

@interface BrideSaidView ()<UITableViewDataSource, UITableViewDelegate, PullTableViewDelegate>
/// 刷新加载
@property (nonatomic, retain) CKTableView *tableView;
/// 数据数组
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation BrideSaidView

- (instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.dataArray = [[NSMutableArray alloc] init];
        [self netWorking];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self createTableView];
}

- (void)netWorking
{
    NSString *urlStr = [NSString stringWithFormat:@"http://hunliji.com/stories.json?page=1&sort=0"];
    [NetHandler networkWithURL:urlStr completion:^(id result) {
        NSArray *array = [result objectForKey:@"stories"];
        for (NSDictionary *dic in array) {
            BrideSaidModel *model = [[BrideSaidModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            BSUres *user = [[BSUres alloc] init];
            /// 运行时model.user为字典, 警告是因为编译时是BSUres类型的;
            [user setValuesForKeysWithDictionary:model.user];
            model.user = user;
            
            [self.dataArray addObject:model];
//            NSLog(@"作者====%@", user.nick);
        }
        [self.tableView reloadData];
    }];
}

- (void)createTableView
{
    self.tableView = [[CKTableView alloc]initWithFrame:self.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.pullDelegate = self;
    [self addSubview:self.tableView];
}

#pragma mark -
#pragma mark 加载刷新
- (void) refreshTable
{
    [self.tableView reloadData];
    
    self.tableView.pullLastRefreshDate = [NSDate date];
    self.tableView.pullTableIsRefreshing = NO;
}

- (void) loadMoreDataToTable
{
    //    //  加载更多实现代码放在在这
    [self shuaxinNetWork];
    self.tableView.pullTableIsLoadingMore = NO;
}
/// 加载更多
///  刷新方法
static NSInteger count = 2;
- (void)shuaxinNetWork
{
    NSString *hotUrl = [NSString stringWithFormat:@"http://hunliji.com/stories.json?page=%ld&sort=0", count];
    [NetHandler networkWithURL:hotUrl completion:^(id result) {
        NSArray *array = [result objectForKey:@"stories"];
        for (NSDictionary *dic in array) {
            BrideSaidModel *model = [[BrideSaidModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            BSUres *user = [[BSUres alloc] init];
            /// 运行时model.user为字典, 警告是因为编译时是BSUres类型的;
            [user setValuesForKeysWithDictionary:model.user];
            model.user = user;
            [self.dataArray addObject:model];
//            NSLog(@"作者====%@", user.nick);
        }
        [self.tableView reloadData];
    }];
    count ++;
}
#pragma mark -
#pragma mark - PullTableViewDelegate协议

- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0f];
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0f];
}

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#warning 进度 自定义cell
    static NSString *string = @"BrideSaid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    return cell;
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.width * 0.5;
}
///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
